package cn.qiao.fs;

import cn.qiao.fs.esl.inbound.Client;
import cn.qiao.fs.esl.inbound.IEslEventListener;
import cn.qiao.fs.esl.internal.Context;
import cn.qiao.fs.esl.internal.IModEslApi;
import cn.qiao.fs.esl.transport.event.EslEvent;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

public class ClientExample {
    private static final Logger logger = LoggerFactory.getLogger(ClientExample.class);

    public static void main(String[] args) {
        try {


            String password = "1234\n\n";
            Client client = new Client();

            client.addEventListener(new IEslEventListener() {
                @Override
                public void onEslEvent(Context ctx, EslEvent event) {
                    logger.info("Received event: {}", event.getEventName());
                }

                @Override
                public void onClose(Channel channel) {
                    System.out.println("Closing channel");
                }
            });

            client.connect(new InetSocketAddress("localhost", 8021), password, 10);
            client.setEventSubscriptions(IModEslApi.EventFormat.PLAIN, "all");
            //这里必须检查，防止网络抖动时，连接断开
            if (client.canSend()) {
                System.out.println("连接成功，准备发起呼叫...");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

}
